$(function () {


    $.post('/qutu/Api/jsApi/', {
        },

        function (res) {
            if (res.status != 0) {
                alert('服务器开小差啦，再试一下吧.');
                return;
            }

            wx.ready(function () {
                wx.showOptionMenu();
            });

            wx.config(res.data.wechatJsApiConfig);
            wx.error(function(res){
                //location.href = "http://wx.xxjr.com/qutu/api/set_weixinid?secret=ebb1c3692dc07be30e6652adbce81692&pn=2";
                // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
                if(is_set_info==1) {
                    $.post('/qutu/api/set_weixinid', {
                            secret: MD5
                        },

                        function (res) {
                            //console.log(res);
                        }, 'json');
                }
            });

        }, 'json');
    $.post('/qutu/api/delete/', {
        },

        function (res) {
            //console.log(res);
        }, 'json');

        document.ready  = function () {
            var button = document.getElementById('createButton');
            button.click();//执行执行点击按钮
        };
    $('#createButton').on('click', function () {
        //$("#info-img").removeChild();
        if($('#agreeBtn').data('status') ==0){
            alert('请同意展业海报服务协议');
            return;
        }
        setLoading();
        var name= $.trim($("#parameter-name").val()) == '' ? $.trim($("#parameter-name").attr('placeholder')) : $.trim($("#parameter-name").val());
        var mobile= $.trim($("#parameter-mobile").val()) == '' ? $.trim($("#parameter-mobile").attr('placeholder')) : $.trim($("#parameter-mobile").val());
        var company= $.trim($("#parameter-company").val()) == '' ? $.trim($("#parameter-company").attr('placeholder')) : $.trim($("#parameter-company").val());
        var email= $.trim($("#parameter-email").val()) == '' ? $.trim($("#parameter-email").attr('placeholder')) : $.trim($("#parameter-email").val());
        var job= $.trim($("#parameter-job").val()) == '' ? $.trim($("#parameter-job").attr('placeholder')) : $.trim($("#parameter-job").val());
        var address= $.trim($("#parameter-address").val()) == '' ? $.trim($("#parameter-address").attr('placeholder')) : $.trim($("#parameter-address").val());
        var info= $.trim($("#parameter-info").val()) == '' ? $.trim($("#parameter-info").attr('placeholder')) : $.trim($("#parameter-info").val());
        var city= $.trim($("#parameter-city").val()) == '' ? $.trim($("#parameter-city").attr('placeholder')) : $.trim($("#parameter-city").val());
        var catgory= $.trim($("#parameter-catgory").val()) == '' ? $.trim($("#parameter-catgory").attr('placeholder')) : $.trim($("#parameter-catgory").val());
        var title= $.trim($("#parameter-title").val()) == '' ? $.trim($("#parameter-title").attr('placeholder')) : $.trim($("#parameter-title").val());
        var advantage1= $.trim($("#parameter-advantage1").val()) == '' ? $.trim($("#parameter-advantage1").attr('placeholder')) : $.trim($("#parameter-advantage1").val());
        var advantage2= $.trim($("#parameter-advantage2").val()) == '' ? $.trim($("#parameter-advantage2").attr('placeholder')) : $.trim($("#parameter-advantage2").val());
        var interest= $.trim($("#parameter-interest").val()) == '' ? $.trim($("#parameter-interest").attr('placeholder')) : $.trim($("#parameter-interest").val());
        var limit= $.trim($("#parameter-limit").val()) == '' ? $.trim($("#parameter-limit").attr('placeholder')) : $.trim($("#parameter-limit").val());
        var time= $.trim($("#parameter-time").val()) == '' ? $.trim($("#parameter-time").attr('placeholder')) : $.trim($("#parameter-time").val());
        var require= $.trim($("#parameter-require").val()) == '' ? $.trim($("#parameter-require").attr('placeholder')) : $.trim($("#parameter-require").val());
        var apply= $.trim($("#parameter-apply").val()) == '' ? $.trim($("#parameter-apply").attr('placeholder')) : $.trim($("#parameter-apply").val());
        var mortgage= $.trim($("#parameter-mortgage").val()) == '' ? $.trim($("#parameter-mortgage").attr('placeholder')) : $.trim($("#parameter-mortgage").val());
        var age= $.trim($("#parameter-age").val()) == '' ? $.trim($("#parameter-age").attr('placeholder')) : $.trim($("#parameter-age").val());

        var dc_city= $.trim($("#parameter-dc_city").val()) == '' ? $.trim($("#parameter-dc_city").attr('placeholder')) : $.trim($("#parameter-dc_city").val());
        var dc_area= $.trim($("#parameter-dc_area").val()) == '' ? $.trim($("#parameter-dc_area").attr('placeholder')) : $.trim($("#parameter-dc_area").val());
        var dc_buy_time= $.trim($("#parameter-dc_buy_time").val()) == '' ? $.trim($("#parameter-dc_buy_time").attr('placeholder')) : $.trim($("#parameter-dc_buy_time").val());
        var house_area_1= $.trim($("#parameter-house_area_1").val()) == '' ? $.trim($("#parameter-house_area_1").attr('placeholder')) : $.trim($("#parameter-house_area_1").val());
        var house_area_2= $.trim($("#parameter-house_area_2").val()) == '' ? $.trim($("#parameter-house_area_2").attr('placeholder')) : $.trim($("#parameter-house_area_2").val());
        var site_url= $.trim($("#parameter-site-url").val()) == '' ? $.trim($("#parameter-site-url").attr('placeholder')) : $.trim($("#parameter-site-url").val());
        var recommend_id=$("#recommend_id").val();
        var sex= $.trim($("#parameter-sex").val()) == '' ? $.trim($("#parameter-sex").attr('placeholder')) : $.trim($("#parameter-sex").val());
        var store_name= $.trim($("#parameter-store-name").val()) == '' ? $.trim($("#parameter-store-name").attr('placeholder')) : $.trim($("#parameter-store-name").val());
        var store_motto= $.trim($("#parameter-store-motto").val()) == '' ? $.trim($("#parameter-store-motto").attr('placeholder')) : $.trim($("#parameter-store-motto").val());
        var system_time= $.trim($("#parameter-system-time").val()) == '' ? $.trim($("#parameter-system-time").attr('placeholder')) : $.trim($("#parameter-system-time").val());
        var current_stage= $.trim($("#parameter-current-stage").val()) == '' ? $.trim($("#parameter-current-stage").attr('placeholder')) : $.trim($("#parameter-current-stage").val());
        var current_status= $.trim($("#parameter-current-status").val()) == '' ? $.trim($("#parameter-current-status").attr('placeholder')) : $.trim($("#parameter-current-status").val());
        var credit_status= $.trim($("#parameter-credit-status").val()) == '' ? $.trim($("#parameter-credit-status").attr('placeholder')) : $.trim($("#parameter-credit-status").val());
        var apply_number= $.trim($("#parameter-apply-number").val()) == '' ? $.trim($("#parameter-apply-number").attr('placeholder')) : $.trim($("#parameter-apply-number").val());
        var apply_date= $.trim($("#parameter-apply-date").val()) == '' ? $.trim($("#parameter-apply-date").attr('placeholder')) : $.trim($("#parameter-apply-date").val());
        var apply_amount= $.trim($("#parameter-apply-amount").val()) == '' ? $.trim($("#parameter-apply-amount").attr('placeholder')) : $.trim($("#parameter-apply-amount").val());
        var approval_amount= $.trim($("#parameter-approval-amount").val()) == '' ? $.trim($("#parameter-approval-amount").attr('placeholder')) : $.trim($("#parameter-approval-amount").val());
        var loan_amount= $.trim($("#parameter-loan-amount").val()) == '' ? $.trim($("#parameter-loan-amount").attr('placeholder')) : $.trim($("#parameter-loan-amount").val());
        var credit_period= $.trim($("#parameter-credit-period").val()) == '' ? $.trim($("#parameter-credit-period").attr('placeholder')) : $.trim($("#parameter-credit-period").val());
        var premium_rate= $.trim($("#parameter-premium-rate").val()) == '' ? $.trim($("#parameter-premium-rate").attr('placeholder')) : $.trim($("#parameter-premium-rate").val());
        var remark= $.trim($("#parameter-remark").val()) == '' ? $.trim($("#parameter-remark").attr('placeholder')) : $.trim($("#parameter-remark").val());
        var select_test= $.trim($("#parameter-test-select").val()) == '' ? $.trim($("#parameter-test-select").attr('placeholder')) : $.trim($("#parameter-test-select").val());
        var id= $('#id').val();
        var pn=$("#pn").val();	//公众号
        if(name.length > 4){
            alert('姓名不能多于4个字！');
            return false;
        }
        if(company.length > 32){
            alert('公司不能多于32个字');
            return false;
        }
        if(email.length > 32){
            alert('邮箱不能多于32个字');
            return false;
        }
        if(job.length > 16){
            alert('职位不能多于16个字');
            return false;
        }
        if(address.length > 50){
            alert('地址不能多于50个字');
            return false;
        }
        if(info.length > 400){
            alert('自我介绍不能多于400个字');
            return false;
        }
        if(mobile) {
            //var ze = /^1[1,2,3,4,5,6,7,8,9]\d{9}$/;
            var ze =/^(1[1,2,3,4,5,6,8,7,9]{1}[\d]{9})|(((400)(\d{3})(\d{4}))|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
            if (!ze.test(mobile)) {
                alert('您输手机号码格式不正确！');
                //layer.msg('您输手机号码格式不正确！', 2, -1); //2秒后自动关闭，-1代表不显示图标
                return false;
            }
        }
        $.post('/qutu/index/execute', {
                id:id,
                name: name,
                mobile: mobile,
                company: company,
                email: email,
                job: job,
                address:address,
                info:info,
                city:city,
                catgory:catgory,
                pn:pn,
                title:title,
                advantage1:advantage1,
                advantage2:advantage2,
                interest:interest,
                limit:limit,
                time:time,
                require:require,
                apply:apply,
                mortgage:mortgage,
                age:age,
                dc_city:dc_city,
                dc_area:dc_area,
                dc_buy_time:dc_buy_time,
                house_area_1:house_area_1,
                house_area_2:house_area_2,
                site_url:site_url,
                recommend_id:recommend_id,
                sex:sex,
                store_name:store_name,
                store_motto:store_motto,
                system_time:system_time,
                current_stage:current_stage,
                current_status:current_status,
                remark:remark,
                premium_rate:premium_rate,
                credit_period:credit_period,
                loan_amount:loan_amount,
                approval_amount:approval_amount,
                apply_date:apply_date,
                apply_amount:apply_amount,
                apply_number:apply_number,
                credit_status:credit_status,
                select_test:select_test,
            },
            function (res) {



                if (!res.status ) {

                    if (res.message) {
                        clearLoading();
                        alert(res.message);
                    } else {
                        clearLoading();
                        alert('服务器开小差啦，再试一下吧');
                    }

                    return;
                }

                //$("#result").html("");

                var images = res.data.images;
                var is_card = res.is_card;
                var pn = res.pn;
                var id = res.id;
                var recommend_id=$("#recommend_id").val();	//公众号
                var buy_url = res.buy_url;
                var images_length=images.length;
                if(images_length==1){
                    $('#info-img-1').attr('src',images[0]);
                    $('#info-img-1').live('click',function () {
                        wx.previewImage({
                            current: images[0],
                            urls: images
                        });
                    });

                    document.getElementById("info-img-1").onload = function () {
                        clearLoading();
                    }

                }else {

                    $("#result").html("");
                    images.forEach(function (image, i) {
                        if (i == 0) {
                            if(is_pay==2){

                            }else {
                                $('<p id="info-img" >图' + (i + 1) + ' 长按保存后使用  <a class="ui-link" id="update_order" style="float: right;font-weight: normal" href="javascript:void(0);">已购买还有水印?</a></p>').appendTo($("#result"));
                            }
                            $('<img/>', {
                                src: image,
                                width: '80%',

                                id: 'info-img',
                                click: function () {
                                    wx.previewImage({
                                        current: image,
                                        urls: images
                                    });
                                }
                            }).appendTo($("#result"));
                        } else {
                            if(is_pay==2){

                            }else {
                                $('<p id="info-img" >图' + (i + 1) + ' 长按保存后使用</p>').appendTo($("#result"));
                            }
                            $('<img/>', {
                                src: image,
                                width: '80%',

                                id: 'info-img',
                                click: function () {
                                    wx.previewImage({
                                        current: image,
                                        urls: images
                                    });
                                }
                            }).appendTo($("#result"));
                        }

                        if (is_card) {
                            if (buy_url != 0) {
                                $('<p  style="text-align: center">这个名片牛逼吗?</p><p  style="text-align: center">想要实物版的名片吗?</p><a href=' + buy_url + '><img width="100%" src="/qutu/Public/images/button_buy.gif"></img></a>').appendTo($("#result"));
                            }

                        }

                    });

                //$('<div style="clear: both"></div><a target="_blank" style="    font-size: 16px;color: #ffffff;font-weight: normal;text-align: center;display: block;text-decoration: none;background: #e1642f;    margin: 2rem 5.5rem;;line-height: 1.9rem;border-radius: .3rem;    text-shadow: 0 0 0 #fff;" href="http://wx.xxjr.com/qutu/l/iscard/0/pn/'+ pn +'/recommend_id/'+ recommend_id +'">点击生成更多海报</a>').appendTo($("#result"));

                    clearLoading();
                }

                $('#update_order').off().on('click', function () {
                    var _this = $(this);
                    var id=$("#id").val();
                    var pn=$("#pn").val();
                    $.ajax({
                        type: 'post',
                        url:'/qutu/api/update_order',
                        data:{id: id,pn:pn},
                        success:function(res){
                            var  res=JSON.parse(res);
                            if(res.status) {
                                alert(res.message);
                                return false;
                            }
                            else {
                                alert(res.message);
                                return false;
                            }
                        }
                    })
                });
                showhot();
                $(".recommend").show();
            },
            'json'
        ).error(function () {
            clearLoading();
            //
            //alert('服务器开小差啦，再试一下吧');
        });

    });



    //function onBridgeReady(){
    //    WeixinJSBridge.call('hideOptionMenu');
    //};
    //if (typeof WeixinJSBridge == "undefined"){
    //    if( document.addEventListener ){
    //        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
    //    }else if (document.attachEvent){
    //        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
    //        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
    //    }
    //}else{
    //    onBridgeReady();
    //}

    function wxpay(payParams){
        //console.log(payParams.appId);
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', {
                "appId": payParams.appId+"",
                "timeStamp":payParams.timeStamp+"",
                "nonceStr":payParams.nonceStr+"",
                "package":payParams.package+"",
                "signType":payParams.signType+"",
                "paySign":payParams.paySign+""
            },
            function(res){
                if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                    var id= $('#id').val();
                    var pn=$("#pn").val();	//公众号
                    var time=$("#time").val();	//公众号
                    var recommend_id=$("#recommend_id").val();	//公众号
                    if(id==99999997){
                        location.href ="http://wx.xxjr.com/qutu/active/new_sign_today?pn="+pn+"&time="+time+"&recommend_id="+recommend_id;
                    }else {
                        location.href = "http://wx.xxjr.com/qutu/e/id/" + id+"?pn="+pn+"&time="+time+"&recommend_id="+recommend_id;
                    }
                }else if(res.err_msg == "get_brand_wcpay_request:fail"){
                    alert("微信支付失败", {title : '错误提示'});
                }
            }
        );
    }
    var rand=0;
    $('#more_1').on('click',function(){
        var pn = $('#pn').val();

        if(pn==1){
            var lists='lists';
        }else if(pn==2){
            var lists='dclists';
        }else if(pn==3){
            var lists='bxlists';
        }
        $.ajax({
            type: 'post',
            url:'/qutu/more',
            data:{pn: pn,rand:rand},
            success:function(res){
                var data = JSON.parse(res);
                var recommend_id=$("#recommend_id").val();
                var html = '';
                    rand++;
                for(var i=0;i<data.length;i++){
                    var vo = data[i];
                    html += '<div class="recommend-item xxjr-fl"><a href="'+vo.url+'?pn='+pn+'&recommend_id='+ recommend_id +'" target="_blank">' +
                        '<div style="height: 10rem;overflow: hidden;width: 100%"><img src="'+cdn_img_url+'/qutu/Public/images/'+lists+'/small' + vo.id + '.jpg"></div>' +
                        '<div class="text">'+vo.title+'</div>' ;

                    if(vo.is_pay==1){
                        html += '<div><p class="xxjr-fl" style="font-size: .84rem;color: #ed662c;    margin-left: 0.0rem;font-weight: normal"><i class="money"></i>'+vo.pay_money+'</p>';
                    }else{
                        html += '<div><p class="xxjr-fl" style="font-size: .84rem;color: #4995cf;    margin-left: 0.0rem;font-weight: normal">免费</p>';
                    }
                    html += '<p  style="float: right;font-size: 0.84rem;color: #333;font-weight: normal;"><i class="la"></i> '+ vo.make_time +'</p></div>';

                    html += '</a></div>';



                }
                $('.recommend-list').html(html);
                if(data[2]['rand']>20){
                    rand=0;
                }
            }
        })
    })
    $('#JS-protocol').on('click', function () {
        $('.agreement-box').show();
    });
    $('.agreement-box .header-left').on('click',function(){
        $('.agreement-box').hide();
    });
    $('#memberButton').on('click', function () {
        if($('#agreeBtn').data('status') ==0){
            alert('请同意展业海报服务协议');
            return;
        }
        var pn=$("#pn").val();	//公众号
        var  recommend_id=$("#recommend_id").val();
       window.location.href="https://wx.xxjr.com/qutu/vip?pn="+pn+"&recommend_id="+recommend_id;
    });
    $('.modal-box .header-left').on('click',function(){
        $('.modal-box').hide();
    });

    $('.modal-box .recharge-cell').off().on('click',function(){
        $('.modal-box .recharge-cell.active').removeClass('active');
        $(this).addClass('active');
        if(!is_weixin){
            alert('请用微信扫一扫扫码本页底部二维码，关注公众号使用此功能');
            return
        }
        // 微信支付
        var id= 99999999;

        var pn=$("#pn").val();	//公众号
        var mobile= $.trim($("#parameter-mobile").val()) == '' ? $.trim($("#parameter-mobile").attr('placeholder')) : $.trim($("#parameter-mobile").val());
        var members_day= $(this).data('days');
        var  recommend_id=$("#recommend_id").val();
        $.post('/qutu/pay', {
                id:id,
                pn:pn,
                mobile:mobile,
                members_day:members_day,
                recommend_id:recommend_id,
            },

            function (data) {
                if (data.success) {
                    //console.log(data);
                    //$('.modal').show();
                    //$('#modal').html('当前下单手机号为:'+mobile);
                    //$('.modal .confirm').on('click', function(){
                    //    $('.modal').hide()
                        var payParams = JSON.parse(data.data);
                        wxpay(payParams);
                    //});
                    //$('.modal .cancel').on('click', function(){
                    //    $('.modal').hide()
                    //});
                } else {
                    if(data.status) {

                        if(window.confirm(data.msg)){
                            //alert("确定");
                            $.post('/qutu/pay', {
                                    id:id,
                                    pn:pn,
                                    mobile:mobile,
                                    members_day:members_day,
                                    recommend_id:recommend_id,
                                    type:1,
                                },

                                function (data) {
                                    if (data.success) {
                                        alert(data.msg);
                                        if(is_set_info==2){
                                            location.href = "http://wx.xxjr.com/qutu/active/success?pn="+pn;
                                        }
                                        location.href = "http://wx.xxjr.com/qutu/l?pn="+pn+"&time="+time+"&recommend_id="+recommend_id;

                                    } else {
                                        alert(data.msg);
                                    }

                                }, 'json');
                            return true;
                        }else{
                            $.post('/qutu/pay', {
                                    id:id,
                                    pn:pn,
                                    mobile:mobile,
                                    members_day:members_day,
                                    recommend_id:recommend_id,
                                    type:0,
                                    is_commission_balance:1,
                                },

                                function (data) {
                                    if (data.success) {

                                        var payParams = JSON.parse(data.data);
                                        wxpay(payParams);

                                    } else {
                                        alert(data.msg);
                                    }

                                }, 'json');
                            return false;
                        }
                    }else {
                        alert(data.msg);
                    }
                }

            }, 'json');
    })

    $('#payButton').off().on('click', function () {
        if($('#agreeBtn').data('status') ==0){
            alert('请同意展业海报服务协议');
            return;
        }
        if(!is_weixin){
            alert('请用微信扫一扫扫码本页底部二维码，关注公众号使用此功能');
            return
        }
        // 微信支付
        var id= $('#id').val();
        var pn=$("#pn").val();	//公众号
        var num=$("#num").val();
        var recommend_id=$("#recommend_id").val();
        var mobile= $.trim($("#parameter-mobile").val()) == '' ? $.trim($("#parameter-mobile").attr('placeholder')) : $.trim($("#parameter-mobile").val());
        $('.modal').show();
        $('#modal').html('当前下单手机号为:'+mobile);
        $('.modal .confirm').off().on('click', function(){
            $('.modal').hide()
        $.post('/qutu/pay', {
                id:id,
                pn:pn,
                mobile:mobile,
                recommend_id:recommend_id,
                num:num
            },

            function (data) {
                if (data.success) {
                    //console.log(data);
                        var payParams = JSON.parse(data.data);
                        wxpay(payParams);
                } else {
                    alert(data.msg);
                }

            }, 'json');
        });
        $('.modal .cancel').off().on('click', function(){
            $('.modal').hide()
        });



    });

    $('#signButton').off().on('click', function () {

        if(!is_weixin){
            alert('请用微信扫一扫扫码本页底部二维码，关注公众号使用此功能');
            return
        }
        // 微信支付
        var id= 99999997;//打卡挑战金
        var pn=$("#pn").val();	//公众号
        var recommend_id=$("#recommend_id").val();
        $('.modal').show();
        $('#modal').html('你确定已经了解挑战规则<br>并且支付10元参与挑战吗?<br>挑战会在第一次打卡成功后开启');
        $('.modal .confirm').off().on('click', function(){
            $('.modal').hide()
            $.post('/qutu/pay', {
                  id:id,
                  pn:pn,
                  mobile:13333333333,
                  recommend_id:recommend_id,
                  num:1,
              },

              function (data) {
                  if (data.success) {
                      //console.log(data);
                      var payParams = JSON.parse(data.data);
                      wxpay(payParams);
                  } else {
                      alert(data.msg);
                  }

              }, 'json');
        });
        $('.modal .cancel').off().on('click', function(){
            $('.modal').hide()
        });



    });

    $('#inviteButton').off().on('click', function () {
        if($('#agreeBtn').data('status') ==0){
            alert('请同意展业海报服务协议');
            return;
        }
        if(!is_weixin){
            alert('请用微信扫一扫扫码本页底部二维码，关注公众号使用此功能');
            return
        }
        // 微信支付
        var id= $('#id').val();
        var time=$("#time").val();	//公众号
        var pn=$("#pn").val();	//公众号
        var recommend_id=$("#recommend_id").val();
        var mobile= $.trim($("#parameter-mobile").val()) == '' ? $.trim($("#parameter-mobile").attr('placeholder')) : $.trim($("#parameter-mobile").val());
        $('.modal2').show();
        $('#modal2').html('当前下单手机号为:'+mobile);
        $('.modal2 .confirm').off().on('click', function(){
            $('.modal2').hide()
        $.post('/qutu/pay', {
                id:id,
                pn:pn,
                mobile:mobile,
                recommend_id:recommend_id,
                type:1,
            },

            function (data) {
                if (data.success) {
                    //console.log(data);
                    alert(data.msg);
                    location.href = "http://wx.xxjr.com/qutu/e/id/" + id+"?pn="+pn+"&time="+time+"&recommend_id="+recommend_id;
                } else {
                    //alert(data.msg);


                    if(window.confirm(data.msg)){
                        //alert("确定");
                        var payParams = JSON.parse(data.data);
                        wxpay(payParams);
                        return true;
                    }else{
                        //alert("取消");
                        return false;
                    }
                }

            }, 'json');

        });
        $('.modal2 .cancel').off().on('click', function(){
            $('.modal2').hide()
        });


    });
});



function setLoading() {
    var name= $.trim($("#parameter-name").val()) == '' ? $.trim($("#parameter-name").attr('placeholder')) : $.trim($("#parameter-name").val());
    var mobile= $.trim($("#parameter-mobile").val()) == '' ? $.trim($("#parameter-mobile").attr('placeholder')) : $.trim($("#parameter-mobile").val());
    var company= $.trim($("#parameter-company").val()) == '' ? $.trim($("#parameter-company").attr('placeholder')) : $.trim($("#parameter-company").val());
    var email= $.trim($("#parameter-email").val()) == '' ? $.trim($("#parameter-email").attr('placeholder')) : $.trim($("#parameter-email").val());
    var job= $.trim($("#parameter-job").val()) == '' ? $.trim($("#parameter-job").attr('placeholder')) : $.trim($("#parameter-job").val());
    var address= $.trim($("#parameter-address").val()) == '' ? $.trim($("#parameter-address").attr('placeholder')) : $.trim($("#parameter-address").val());
    var info= $.trim($("#parameter-info").val()) == '' ? $.trim($("#parameter-info").attr('placeholder')) : $.trim($("#parameter-info").val());

    if(name.length > 4){
        return false;
    }
    if(company.length > 32){
        return false;
    }
    if(email.length > 32){
        return false;
    }
    if(job.length > 16){
        return false;
    }
    if(address.length > 50){
        return false;
    }
    if(info.length > 400){
        return false;
    }
    if(mobile) {
        var ze =/^(1[1,2,3,4,5,6,8,7,9]{1}[\d]{9})|(((400)(\d{3})(\d{4}))|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{3,7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
        if (!ze.test(mobile)) {
            alert('您输手机号码格式不正确！');
            //layer.msg('您输手机号码格式不正确！', 2, -1); //2秒后自动关闭，-1代表不显示图标
            return false;
        }
    }
    $('#createButton').attr('disabled', true);

    $.mobile.loading("show", {
        theme: 'a',
        text: '正在生成趣图',
        textVisible: true,
        textOnly: false
    });

}

function clearLoading() {
    $.mobile.loading("hide");

    $('#createButton').removeAttr('disabled');
}

function showhot() {

    $("#hot").show();

}

function onChooseImageClick(id) {
    wx.chooseImage({
        count: 1,
        success: function (res) {
            var localId = res.localIds[0];
            $('#' + id).attr('src', localId);
            if (localId.indexOf("wxlocalresource") != -1) {
                localId = localId.replace("wxlocalresource", "wxLocalResource");
            }
            wx.uploadImage({
                localId: localId,
                success: function (res) {
                    $('#' + id).attr('serverId', res.serverId);
                    $.post('/qutu/api/wechat/', {
                            avatar:res.serverId
                        },

                        function (res) {
                            console.log(res);
                        }, 'json');
                },
                fail: function (res) {
                    alert('上传失败，请重新上传！');
                }

            });
        }
    });
}
function onChooseImageClick2(id) {
    wx.chooseImage({
        count: 1,
        success: function (res) {
            var localId = res.localIds[0];
            $('#' + id).attr('src', localId);
            if (localId.indexOf("wxlocalresource") != -1) {
                localId = localId.replace("wxlocalresource", "wxLocalResource");
            }
            wx.uploadImage({
                localId: localId,
                success: function (res) {
                    $('#' + id).attr('serverId', res.serverId);
                    $.post('/qutu/api/wechat2/', {
                            avatar:res.serverId
                        },

                        function (res) {
                            console.log(res);
                        }, 'json');
                },
                fail: function (res) {
                    alert('上传失败，请重新上传！');
                }

            });
        }
    });
}

function onRadioClick(id, value) {
    $('#' + id).attr('value', value);
}
function onRadioClick2(id, value) {
    $('#' + id).attr('value', value);
    var button = document.getElementById('createButton');
    button.click();//执行执行点击按钮
    scrollTo(0,0);
}
function onCheckClick(id, classid) {

    var oldvalue = $('#' + id).val();


    var status = $('input[type="checkbox"]').filter('.' + classid).map(function () {
        var value = $(this).attr('value');
        if ($(this).is(':checked')){
            return value;

        }

        else{
            return '';

        }

    });


    var r = '';
    for (var i = 0; i < status.length; i++) {
        console.log('a'+status[i]+'b');

        if(status[i].trim().length>0)
        {
            r = r + status[i].trim();

            r=r+'|';

        }


    }


    $('#' + id).attr('value', r);


}
function count_banner_times(id){

    $.post('/qutu/api/count_banner/', {
            id:id,
            MD5:MD5,
        },

        function (res) {
            //console.log(res);
        }, 'json');
}

function view_image(image){

    var images=new Array();
    images[0]=image;
    wx.previewImage({

        current: image, // 当前显示图片的http链接

        urls: images // 需要预览的图片http链接列表

    });
}
function share_friend(){
    var title_friend=$('#title_friend').val();
    var Newcontent=title_friend.replace(new RegExp("<br>","gm"),",");
    var link_friend=$('#link_friend').val();
    var imgurl_friend=$('#imgurl_friend').val();
    wx.onMenuShareTimeline({

        title: Newcontent, // 分享标题

        link: link_friend, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致

        imgUrl:imgurl_friend, // 分享图标

        success: function () {
            var id=$('#id').val();
            var pn=$("#pn").val();	//公众号
            $.post('/qutu/friend_share_log', {
                    id:id,
                    pn:pn,
                    type:1,
                },

                function (data) {
                    if (data.success) {
                        $(".share-tips").css('display','none');
                       alert(data.message)
                    } else {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    }

                }, 'json');
            // 用户确认分享后执行的回调函数

        },

        cancel: function () {
            // 用户取消分享后执行的回调函数

        }

    });
}


function share_group(){
    var title_friend=$('#title_friend').val();
    var Newcontent=title_friend.replace(new RegExp("<br>","gm"),",");
    var link_friend=$('#link_friend').val();
    var imgurl_friend=$('#imgurl_friend').val();
    wx.onMenuShareAppMessage({

        title: Newcontent, // 分享标题

        desc: Newcontent+"|展业海报", // 分享描述

        link: link_friend, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致

        imgUrl: imgurl_friend, // 分享图标

        type: 'link', // 分享类型,music、video或link，不填默认为link

        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空

        success: function () {
            var id=$('#id').val();
            var pn=$("#pn").val();	//公众号
            $.post('/qutu/friend_share_log', {
                    id:id,
                    pn:pn,
                    type:2,
                },

                function (data) {
                    if (data.success) {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    } else {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    }

                }, 'json');
            // 用户确认分享后执行的回调函数

        },

        cancel: function () {

            // 用户取消分享后执行的回调函数

        }

    });
}

function share_index(){
    var title_friend=$('#title_friend').val();
    var meta_title=$('#meta_title').val();
    var link_friend=$('#link_friend').val();
    var imgurl_friend=$('#imgurl_friend').val();
    wx.onMenuShareAppMessage({

        title: meta_title, // 分享标题

        desc: title_friend, // 分享描述

        link: link_friend, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致

        imgUrl: imgurl_friend, // 分享图标

        type: 'link', // 分享类型,music、video或link，不填默认为link

        dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空

        success: function () {
            var id=$('#id').val();
            var pn=$("#pn").val();	//公众号
            $.post('/qutu/friend_share_log', {
                    id:id,
                    pn:pn,
                    type:2,
                },

                function (data) {
                    if (data.success) {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    } else {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    }

                }, 'json');
            // 用户确认分享后执行的回调函数

        },

        cancel: function () {

            // 用户取消分享后执行的回调函数

        }

    });
}
function share_index_friend(){
    var title_friend=$('#title_friend').val();
    var meta_title=$('#meta_title').val();
    var link_friend=$('#link_friend').val();
    var imgurl_friend=$('#imgurl_friend').val();
    wx.onMenuShareTimeline({

        title: meta_title+" "+title_friend, // 分享标题

        link: link_friend, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致

        imgUrl:imgurl_friend, // 分享图标

        success: function () {
            var id=$('#id').val();
            var pn=$("#pn").val();	//公众号
            $.post('/qutu/friend_share_log', {
                    id:id,
                    pn:pn,
                    type:1,
                },

                function (data) {
                    if (data.success) {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    } else {
                        $(".share-tips").css('display','none');
                        alert(data.message)
                    }

                }, 'json');
            // 用户确认分享后执行的回调函数

        },

        cancel: function () {
            // 用户取消分享后执行的回调函数

        }

    });
}





